{// 引入标签库 }
<tagLib name="html" />
<include file="Public:header" />
<style type="text/css">
	.fLeft{ float:left;}
	.cBoth{clear:both}
	.hMargin{margin-left:3px; margin-right:3px}
	.vMargin{margin-top:3px; margin-bottom:3px}
	#execSql{ font-size:12px; height:198px; width:450px; font-family:verdana,arial;}
	#tableName{ font-size:12px; height:200px; font-family:verdana,arial;}
</style>
<script language="JavaScript">
<!--
	function buildTable(tableId, data){
		for (i = 0; i < data.length; i++) {
			var table = document.getElementById(tableId);
			if (table) {
				var row = table.insertRow(-1);
				if (i == 0) {
					row.className = 'over';
				}
				for (j = 0; j < data[i].length; j++) {
					var cell = document.createElement("td");
					cell.innerHTML = data[i][j];
					row.appendChild(cell);
				}
			}
		}
	}
	function deleteTable(tableId){
		var Table	=	document.getElementById(tableId);
		if (Table) {
			while (Table.rows.length > 0) {
				Table.deleteRow(0);
			}
		}
	}

	function handleSql(data){
		document.getElementById('resultNum').innerHTML=	 '';
		deleteTable('table1');
		deleteTable('table2');
		
		var time = data[0];
		var bench = data[1];
		if (bench) {
			if (bench[0]) {
				document.getElementById('benchbox').style.display = 'block';
				buildTable('table2', bench[0]);
			}
			else {
				document.getElementById('benchbox').style.display = 'none';
			}
		}
		
		var db_data = new Array();
		for(var i=2,j=0;i<data.length;i++,j++)
		{
			db_data[j] = data[i];
		}
		
		if(data.length<=1) {
			document.getElementById('resultNum').innerHTML	=	'[ 返回0条查询记录 耗时:'+time+' ] ';
		}else {
			document.getElementById('resultNum').innerHTML	=	'[ 返回'+(data.length-1)+'条查询记录 耗时:'+time+' ] ';
			buildTable('table1',db_data);
		}
	}
	function runSql(){	
		$.ajax({ 
				url: ROOT+"?"+VAR_MODULE+"="+MODULE_NAME+"&"+VAR_ACTION+"=execute", 
				data: 'ajax=1&sql='+encodeURIComponent(document.getElementById('execSql').value),
				dataType: "json",
				success: function(obj){
					
					$("#info").html(obj.info);
					$("#info").fadeIn();
					$("#info").oneTime(2000, function() {				    
						$(this).fadeOut(2,function(){
							$("#info").html("");
							handleSql(obj.data);
						});			    	
					});	
					
							
				}
		});	
	}

		

	function refleshDb(db){
		$("#useDb").html(db);
		$.ajax({ 
				url: ROOT+"?"+VAR_MODULE+"="+MODULE_NAME+"&"+VAR_ACTION+"=getTables", 
				data: 'ajax=1&db='+db,
				dataType: "json",
				success: function(obj){
					data = obj.data;
					fleshTable(data,obj.status);		
				}
		});	
		
	}
	function fleshTable(data,status){
		if (status==1)
		{
			$("#tableName").html("");
			$.each(data, function(i, n){
			  	$("#tableName").append("<option>"+n+"</option>");
			});

		}

	}
//-->
</script>
<!-- 菜单区域  -->

<!-- 主页面开始 -->
<div id="main" class="main" >
<!-- 主体内容  -->
<div class="main_title">SQL语句操作 <span style="font-weight:normal;">[当前数据库:<span style="color:blue" id="useDb">{$Think.session.useDb}</span>]</span></div>
<div class="blank5"></div>
<!--  功能操作区域  -->
<div class="cBoth">
<!-- SQL执行器 -->
<form method=post action="">
	<fieldset id="querybox" >
<legend style="color:gray">数据库查询窗口
<div class="fLeft">在数据库</div>
<select name="database" class="fLeft hMargin" onchange="refleshDb(this.options[this.options.selectedIndex].value)">
<volist name="dbs" id="db">
<neq name="db.Database" value="information_schema">
<if condition="$db['Database'] eq $useDb">
<option value="{$db.Database}" <eq name="db.Database" value="$useDb">selected</eq>> {$db.Database} </option>
</if>
</neq>
</volist>
</select>
<div class="fLeft">中执行SQL语句（<span style="color:orange">请慎用</span>） </div></legend>
<div class="cBoth"></div>
<div id="dbSql" class="cBoth vMargin">
<div class="fLeft"><TEXTAREA id="execSql" NAME="sql" >{$Think.get.sql}</TEXTAREA></div>
<div id="dbSql" >
<div class="fLeft hMargin">
<select NAME="tableName" id="tableName" class="medium" size="12" multiple >
<iterate name="tables" id="table" >
<option value="{$table}">{$table}</option>
</iterate>
</select></div>
<div class="fLeft" style="width:65px">

</div>
</div>
</div>
<br class="cBoth">
<div class="cBoth vMargin" >
<html:imageBtn name="button" value="执行"  click="runSql()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="button" type="reset" value="重置"  style="impBtn hMargin fLeft shadow" />
</div>
</fieldset>
</form>

<!-- 表操作器 -->
<fieldset id="benchbox" style="display:none">
<legend style="color:gray">数据库性能分析 </legend>
<div id="SqlBench" style="overflow:auto;width:100%;height:100%">
<table id='table2' class="dataTable" style="width:450px"></table>
</div>
</fieldset>
<fieldset id="querybox" >
<legend style="color:gray">数据库查询结果 <span class="blue" id="resultNum"></span> </legend>
<div id="SqlResult" style="overflow:auto;width:100%;height:100%">
<table id='table1' class="dataTable" cellpadding=0 cellspacing=0 ></table>
</div>
</fieldset>
</div>
<!-- 列表显示区域结束 -->

<!-- 主体内容结束 -->
</div>
<!-- 主页面结束 -->
<script language="JavaScript">
<!--
	if ({$Think.get.run|default=0})	runSql();
//-->
</script>
<include file="Public:footer" />